'use strict';

const {
	Controller
} = require('uni-cloud-router');
const {
	getUserInfo
} = require("user-info-admin");
const dbcmd = uniCloud.database().command;

module.exports = class AppController extends Controller {
	// 设置分类导航数量
	async parameter() {
		let response = {
			code: 1,
			datas: ''
		};

		let start = new Date().getTime();
		// 请求参数
		const req = this.ctx.data;
		let uniIdToken = this.ctx.event.uniIdToken
		let userInfo = await getUserInfo(uniIdToken)
		let app_id = userInfo.app_id
		let shop_id = req.shop_id
		const parameterRes = await this.db.collection('wmzmall-app-parameter').where({
			shop_id: shop_id
		}).get();
		if (parameterRes.data && parameterRes.data.length == 1) {
			await this.db.collection('wmzmall-app-parameter').where({
				shop_id: shop_id
			}).update(req);
		} else {
			req.app_id = app_id
			await this.db.collection('wmzmall-app-parameter').add({
				...req
			});
		}

		response.datas = '操作成功';

		let end = new Date().getTime();
		console.log(`耗时：${end - start}ms`);
		response.code = 0;
		response.msg = `耗时：${end - start}ms`;
		return response;
	}
}
